package com.qfedu.buy.common.util;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONObject;
import com.qfedu.buy.common.config.RedisKeyConfig;

import java.util.Base64;

/**
 * @program: NeighborBuy
 * @description:
 * @author: Feri(邢朋辉)
 * @create: 2021-01-25 16:29
 */
public class TokenUtil {
    private static final String KEY="Wv8wmgYxA5h7HQWmzA9HgA==";
    //生成令牌
    public static String createToken(Object obj){
        return SecureUtil.aes(Base64.getDecoder().decode(KEY)).encryptHex(System.currentTimeMillis()+"_"+obj.toString());
    }
    //解析令牌
    public static int getUid(String token){
        if(RedissonUtil.isHave(RedisKeyConfig.USER_TOKEN+token)){
            String json= RedissonUtil.getStr(RedisKeyConfig.USER_TOKEN+token);
            return new JSONObject(json).getInt("id");
        }else {
            return -1;
        }
    }
    //解析令牌
    public static String getToken(String token){
        if(RedissonUtil.isHave(RedisKeyConfig.USER_TOKEN+token)){
            String json= RedissonUtil.getStr(RedisKeyConfig.USER_TOKEN+token);
            return json;
        }else {
            return null;
        }
    }
}
